package md.frolov.bible.commands;

import md.frolov.bible.editors.BibleEditorInput;
import md.frolov.bible.editors.MainEditor;
import md.frolov.bible.model.Chapter;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;

public class PreviousChapter extends AbstractHandler implements IHandler {
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IEditorPart ie = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if(ie==null) return null;
		if(!(ie instanceof MainEditor)) return null;
		
		MainEditor me = (MainEditor) ie;
		Chapter c = ((BibleEditorInput)me.getEditorInput()).getChapter().previousChapter();
		
		GoToURLCommand.executeStatic(c.getUrl());
		
		return null;
	}

}
